{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "F:\\anaconda\\lib\\site-packages\\h5py\\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-1-a0c80e927c8a>:10: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "WARNING:tensorflow:From F:\\anaconda\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please write your own downloading logic.\n",
      "WARNING:tensorflow:From F:\\anaconda\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting C:\\Users\\Administrator\\AI8\\w6\\data\\train-images-idx3-ubyte.gz\n",
      "WARNING:tensorflow:From F:\\anaconda\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting C:\\Users\\Administrator\\AI8\\w6\\data\\train-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From F:\\anaconda\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.one_hot on tensors.\n",
      "Extracting C:\\Users\\Administrator\\AI8\\w6\\data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting C:\\Users\\Administrator\\AI8\\w6\\data\\t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From F:\\anaconda\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n"
     ]
    }
   ],
   "source": [
    "#from __future__ import absolute_import\n",
    "#from __future__ import division\n",
    "#from __future__ import print_function\n",
    "#import argparse\n",
    "#import sys\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "import tensorflow as tf\n",
    "#FLAGS=None\n",
    "mnist_dir=\"C:\\\\Users\\\\Administrator\\\\AI8\\\\w6\\\\data\"\n",
    "mnist=input_data.read_data_sets(mnist_dir,one_hot=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch_size=100             #每个批次的大小\n",
    "n_batch=mnist.train.num_examples//batch_size        #计算一共多少个批次\n",
    "\n",
    "x=tf.placeholder(tf.float32,[None,784])   #输入数据结构\n",
    "y_=tf.placeholder(tf.float32,[None,10])   #输出结构\n",
    "keep_prob=tf.placeholder(tf.float32)\n",
    "lr=tf.Variable(0.001,dtype=tf.float32)\n",
    "H_NN1=600\n",
    "H_NN2=784\n",
    "#定义参数变量结构\n",
    "w1=tf.Variable(tf.truncated_normal([784,H_NN1],stddev=0.1))  \n",
    "#w1 = get_weight([784,H_NN1], 0.1)\n",
    "b1=tf.Variable(tf.random_normal([H_NN1])+0.1)\n",
    "#y=tf.matmul(x,w)+b\n",
    "#隐层和输出层\n",
    "h1=tf.nn.tanh(tf.matmul(x,w1)+b1)   #隐层1\n",
    "h1_drop=tf.nn.dropout(h1,keep_prob)\n",
    "w2=tf.Variable(tf.truncated_normal([H_NN1,H_NN2],stddev=0.1))  \n",
    "#w2 = get_weight([H_NN1,H_NN2], 0.1)\n",
    "b2=tf.Variable(tf.random_normal([H_NN2]))\n",
    "h2=tf.nn.tanh(tf.matmul(h1_drop,w2)+b2)   #隐层2\n",
    "h2_drop=tf.nn.dropout(h2,keep_prob)\n",
    "w3=tf.Variable(tf.truncated_normal([H_NN2,10],stddev=0.1))  \n",
    "#w3 = get_weight([H_NN2,10], 0.1)\n",
    "b3=tf.Variable(tf.random_normal([10])+0.1)\n",
    "y=tf.nn.softmax(tf.matmul(h2_drop,w3)+b3)\n",
    "#y=tf.nn.softmax(tf.matmul(h1,w2)+b2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Iter0,Testing Accuracy0.7709,learning_rate0.001\n",
      "Iter1,Testing Accuracy0.7752,learning_rate0.00095\n",
      "Iter2,Testing Accuracy0.7804,learning_rate0.0009025\n",
      "Iter3,Testing Accuracy0.8736,learning_rate0.000857375\n",
      "Iter4,Testing Accuracy0.8777,learning_rate0.00081450626\n",
      "Iter5,Testing Accuracy0.972,learning_rate0.0007737809\n",
      "Iter6,Testing Accuracy0.9743,learning_rate0.0007350919\n",
      "Iter7,Testing Accuracy0.9772,learning_rate0.0006983373\n",
      "Iter8,Testing Accuracy0.9758,learning_rate0.0006634204\n",
      "Iter9,Testing Accuracy0.9781,learning_rate0.0006302494\n",
      "Iter10,Testing Accuracy0.9792,learning_rate0.0005987369\n",
      "Iter11,Testing Accuracy0.9803,learning_rate0.0005688001\n",
      "Iter12,Testing Accuracy0.9797,learning_rate0.0005403601\n",
      "Iter13,Testing Accuracy0.9795,learning_rate0.0005133421\n",
      "Iter14,Testing Accuracy0.9801,learning_rate0.000487675\n",
      "Iter15,Testing Accuracy0.9811,learning_rate0.00046329122\n",
      "Iter16,Testing Accuracy0.9794,learning_rate0.00044012666\n",
      "Iter17,Testing Accuracy0.9804,learning_rate0.00041812033\n",
      "Iter18,Testing Accuracy0.982,learning_rate0.00039721432\n",
      "Iter19,Testing Accuracy0.9818,learning_rate0.0003773536\n",
      "Iter20,Testing Accuracy0.9819,learning_rate0.00035848594\n",
      "Iter21,Testing Accuracy0.9807,learning_rate0.00034056162\n",
      "Iter22,Testing Accuracy0.9803,learning_rate0.00032353355\n",
      "Iter23,Testing Accuracy0.9817,learning_rate0.00030735688\n",
      "Iter24,Testing Accuracy0.9813,learning_rate0.000291989\n",
      "Iter25,Testing Accuracy0.982,learning_rate0.00027738957\n",
      "Iter26,Testing Accuracy0.9832,learning_rate0.0002635201\n",
      "Iter27,Testing Accuracy0.9825,learning_rate0.00025034408\n",
      "Iter28,Testing Accuracy0.9832,learning_rate0.00023782688\n",
      "Iter29,Testing Accuracy0.9828,learning_rate0.00022593554\n",
      "Iter30,Testing Accuracy0.9836,learning_rate0.00021463877\n",
      "Iter31,Testing Accuracy0.9824,learning_rate0.00020390682\n",
      "Iter32,Testing Accuracy0.9826,learning_rate0.00019371149\n",
      "Iter33,Testing Accuracy0.983,learning_rate0.0001840259\n",
      "Iter34,Testing Accuracy0.9819,learning_rate0.00017482461\n",
      "Iter35,Testing Accuracy0.9832,learning_rate0.00016608338\n",
      "Iter36,Testing Accuracy0.9837,learning_rate0.00015777921\n",
      "Iter37,Testing Accuracy0.9832,learning_rate0.00014989026\n",
      "Iter38,Testing Accuracy0.983,learning_rate0.00014239574\n",
      "Iter39,Testing Accuracy0.9817,learning_rate0.00013527596\n",
      "Iter40,Testing Accuracy0.983,learning_rate0.00012851215\n",
      "Iter41,Testing Accuracy0.9828,learning_rate0.00012208655\n",
      "Iter42,Testing Accuracy0.9834,learning_rate0.00011598222\n",
      "Iter43,Testing Accuracy0.9832,learning_rate0.00011018311\n",
      "Iter44,Testing Accuracy0.9838,learning_rate0.000104673956\n",
      "Iter45,Testing Accuracy0.9827,learning_rate9.944026e-05\n",
      "Iter46,Testing Accuracy0.9836,learning_rate9.446825e-05\n",
      "Iter47,Testing Accuracy0.9833,learning_rate8.974483e-05\n",
      "Iter48,Testing Accuracy0.9831,learning_rate8.525759e-05\n",
      "Iter49,Testing Accuracy0.9836,learning_rate8.099471e-05\n",
      "Iter50,Testing Accuracy0.9835,learning_rate7.6944976e-05\n"
     ]
    }
   ],
   "source": [
    "# 损失函数\n",
    "cross_entropy=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y_,logits=y))\n",
    "#loss=tf.reduce_mean(tf.square(y-y_))\n",
    "#New loss\n",
    "#cross_entropy = loss + tf.add_n(tf.get_collection('losses'))\n",
    "# 下降方法 梯度下降 参数表示训练效率,通常小于1，实践发现参数不能太大，参数太大会导致无法得到正确的训练结果\n",
    "#lr=0.9\n",
    "train_step=tf.train.AdamOptimizer(lr).minimize(cross_entropy)  #优化目标，使损失最小化\n",
    "correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(y_,1))\n",
    "accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "init_op=tf.global_variables_initializer()   # 初始所有变量，这个必须要有\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init_op)\n",
    "    #总共51个周期\n",
    "    for ephoch in range(51):\n",
    "        #学习率从大变小\n",
    "        sess.run(tf.assign(lr,0.001*(0.95**ephoch)))\n",
    "      #总共n_batch个批次   \n",
    "        for batch in range (n_batch):\n",
    "            batch_xs,batch_ys=mnist.train.next_batch(batch_size)\n",
    "            sess.run(train_step,feed_dict={x:batch_xs,y_:batch_ys,keep_prob:1.0})\n",
    "        learning_rate=sess.run(lr)\n",
    "        #训练完一个周期后测试数据准确率\n",
    "        acc=sess.run(accuracy,feed_dict={x:mnist.test.images,y_:mnist.test.labels,keep_prob:1.0})\n",
    "        print(\"Iter\"+str(ephoch)+\",Testing Accuracy\"+str(acc)+\",learning_rate\"+str(learning_rate))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
